﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Security.Cryptography;


namespace HisGL.Crypto
{
    public class CryptoHelper
    {
      /// <summary>  
      /// AES加密算法  
      /// </summary>  
      /// <param name="input">明文字符串</param>  
      /// <returns>字符串</returns>  
      public static string EncryptByAES(string input,string sKey)
      {
          if (sKey.Length != 16)
          {
              throw new Exception("Key长度必须为16位");
          }
          if (string.IsNullOrWhiteSpace(input))
          {
              return input;
          }
          using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
          {
              rijndaelManaged.Mode = CipherMode.CBC;
              rijndaelManaged.Padding = PaddingMode.PKCS7;
              rijndaelManaged.FeedbackSize = 128;

              rijndaelManaged.Key = Encoding.UTF8.GetBytes(sKey);
              rijndaelManaged.IV = Encoding.UTF8.GetBytes(sKey);

              ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(rijndaelManaged.Key, rijndaelManaged.IV);
              using (MemoryStream msEncrypt = new MemoryStream())
              {
                  using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                  {
                      using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                      {
                          swEncrypt.Write(input);
                      }
                      byte[] bytes = msEncrypt.ToArray();
                      return Convert.ToBase64String(bytes);
                  }
              }
          }
      }

      /// <summary>  
      /// AES解密  
      /// </summary>  
      /// <param name="input">密文字节数组</param>  
      /// <returns>返回解密后的字符串</returns>  
      public static string DecryptByAES(string input, string sKey)
      {
          if (sKey.Length != 16)
          {
              throw new Exception("Key长度必须为16位");
          }
   		  
          if (string.IsNullOrWhiteSpace(input))
          {
              return input;
          }
          var buffer = Convert.FromBase64String(input);
          using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
          {
              rijndaelManaged.Mode = CipherMode.CBC;
              rijndaelManaged.Padding = PaddingMode.PKCS7;
              rijndaelManaged.FeedbackSize = 128;

              rijndaelManaged.Key = Encoding.UTF8.GetBytes(sKey);
              rijndaelManaged.IV = Encoding.UTF8.GetBytes(sKey);

              ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(rijndaelManaged.Key, rijndaelManaged.IV);
              using (MemoryStream msEncrypt = new MemoryStream(buffer))
              {
                  using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, decryptor, CryptoStreamMode.Read))
                  {
                      using (StreamReader srEncrypt = new StreamReader(csEncrypt))
                      {
                          return srEncrypt.ReadToEnd();
                      }
                  }
              }
          }
      }

      /// <summary>
      /// MD5
      /// </summary>
      /// <param name="text"></param>
      /// <returns></returns>
      public static string MD5Encrypt32(string text)
      {
        string pwd = "";
        MD5 md5 = MD5.Create();
        byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(text));
        for (int i = 0; i < s.Length; i++)
        {
          pwd = pwd + s[i].ToString("X2");
        }
        return pwd;
      }

      public static string SHA256Encrypt(string text)
      {
          string pwd = "";
          SHA256 md5 = SHA256.Create();
          byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(text));
          for (int i = 0; i < s.Length; i++)
          {
              pwd = pwd + s[i].ToString("X2");
          }
          return pwd;
      }

      public static string SHA256Encrypt(Stream outputStream)
      {
          string pwd = "";
          SHA256 md5 = SHA256.Create();
          byte[] s = md5.ComputeHash(outputStream);
          for (int i = 0; i < s.Length; i++)
          {
              pwd = pwd + s[i].ToString("X2");
          }
          return pwd;
      }

      /// <summary>
      /// 加密
      /// </summary>
      /// <param name="sCodeType"></param>
      /// <param name="sText"></param>
      /// <returns></returns>
      public static string EncodeBase64(string sCodeType, string sText)
      {
        string encode = "";
        byte[] bytes = Encoding.GetEncoding(sCodeType).GetBytes(sText);
        try
        {
          encode = Convert.ToBase64String(bytes);
        }
        catch
        {
          encode = sText;
        }
        return encode;
      }

      /// <summary>
      /// 解码
      /// </summary>
      /// <param name="sCodeType"></param>
      /// <param name="sText"></param>
      /// <returns></returns>
      public static string DecodeBase64(string sCodeType, string sText)
      {
        string decode = "";
        byte[] bytes = Convert.FromBase64String(sText);
        try
        {
          decode = Encoding.GetEncoding(sCodeType).GetString(bytes);
        }
        catch
        {
          decode = sText;
        }
        return decode;
      }

      public static string EncodeBase64(string sText)
      {
        return EncodeBase64("GB2312", sText); //utf-8
      }

      public static string DecodeBase64(string sText)
      {
        return DecodeBase64("GB2312", sText);
      }
    }
}
